package com.health_predict_springboot.controller;

import com.health_predict_springboot.pojo.SysAgeGroup;
import com.health_predict_springboot.service.SysAgeGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/age-group")
public class SysAgeGroupController {

    @Autowired
    private SysAgeGroupService sysAgeGroupService;

    @GetMapping("/{id}")
    public SysAgeGroup getAgeGroupById(@PathVariable("id") Integer id) {
        return sysAgeGroupService.getAgeGroupById(id);
    }

    @GetMapping("/all")
    public List<SysAgeGroup> getAllAgeGroups() {
        return sysAgeGroupService.getAllAgeGroups();
    }

    @PostMapping
    public boolean addAgeGroup(@RequestBody SysAgeGroup ageGroup) {
        Date now = new Date();
        ageGroup.setCreateTime(now);
        ageGroup.setUpdateTime(now);
        return sysAgeGroupService.addAgeGroup(ageGroup);
    }

    @PutMapping
    public boolean updateAgeGroup(@RequestBody SysAgeGroup ageGroup) {
        ageGroup.setUpdateTime(new Date());
        return sysAgeGroupService.updateAgeGroup(ageGroup);
    }

    @DeleteMapping("/{id}")
    public boolean deleteAgeGroup(@PathVariable("id") Integer id) {
        return sysAgeGroupService.deleteAgeGroup(id);
    }

    @GetMapping("/page")
    public List<SysAgeGroup> getAgeGroupsByPage(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(required = false) String groupName,
            @RequestParam(required = false) Integer minAge,
            @RequestParam(required = false) Integer maxAge) {
        Map<String, Object> params = Map.of(
                "groupName", groupName,
                "minAge", minAge,
                "maxAge", maxAge,
                "offset", (pageNum - 1) * pageSize,
                "pageSize", pageSize
        );
        return sysAgeGroupService.getAgeGroupsByPage(params);
    }

    @GetMapping("/count")
    public int getAgeGroupCount(
            @RequestParam(required = false) String groupName,
            @RequestParam(required = false) Integer minAge,
            @RequestParam(required = false) Integer maxAge) {
        Map<String, Object> params = Map.of(
                "groupName", groupName,
                "minAge", minAge,
                "maxAge", maxAge
        );
        return sysAgeGroupService.getAgeGroupCount(params);
    }
}